#!/bin/bash
# ADORE
# Automatic Doris Environment
#
# USAGE:
#	deramp dorisStep  
#	deramp dorisStep:filename [-o ORDER -e ESTIMATEFILE]
#
# DESCRIPTION:
#  DERAMP is an internal ADORE command.  
#  It removes a first or second order plane from DORIS results using python, numpy, scipy. 
# 
#  The deramp module is located in $ADOREFOLDER/lib/python and has more 
#  adjustable parameters compared to this ADORE function.
#
# INPUT:
#  dorisStep[:filename]
#       The step for the output file. Optionally a different file can be 
#	specified separated by a colon (:).
#
#  Optional:
#	-o ORDER: The order of the polynomial defining the plane. 
#           Can be 1 or 2. Default is 1.
#       -e ESTIMATEFILE: When specified this file is subtracted before calculating
#           the best fitting plane. It has to be the same format as the input.
#
# OUTPUT:
#   Writes the plane removed result with a deramp suffix. 
#
# EXAMPLES:
# deramp unwrap -o 2 
# deramp unwrap -e ${master}_${slave}.crd

#echo $@
#echo $#
#Clear Script Variables
local generate dorisStep remainder #format grepM grepS grepI
local generateCheck #resultFile firstPixel lastPixel width
local fileName resFile format width numlines inputFileName
local m w f e q o c s inArg parE parQ parO parC parS flag OPTIND OPTARG
local cmd passOn lenRemainder
###### GET INPUT OPTIONS
if [ $# -gt 0 ];then
  dorisStep=${1};shift
  remainder=$@
else
  error "Please check usage by: ? deramp"
  return
fi
passOn=()
while [ -n "${remainder}" ]; 
do
  inArg="${remainder##*-}"
  if [ -z "${inArg}" ]; then
    inArg=${remainder};
    remainder=""
  else
    inArg="-${inArg}"
  fi
  lenRemainder=$(( ${#remainder} - ${#inArg} ))
  remainder="${remainder:0:${lenRemainder}}"
  unset OPTIND
  while getopts ":w:f:m:" flag ${inArg};
  do
    case $flag in
      w|f|m)
        eval ${flag}=${OPTARG};;
      *)
        passOn+=($inArg)
        break
      ;;
    esac
  done
done

if [ "${m}" == "network" ]; then 
  _ipy_p="${dorisStep}"
  openInIpython "${ADOREFOLDER}/lib/python/fun/network_deramp_complex.py"  
  return $?
fi
# make generate lowercase to accommadate user entries in caps from APM
remainder="${passOn[@]}" #set remainder to passed on variables for cpxfiddle
if [ "${remainder:-undef}" != "undef" ]; then
  error "I'll pass these options to deramp: ..${remainder}.."
fi

#check if filename is specified with dorisStep
if [[ "${dorisStep}" == *:* ]]; then
  dorisStep="${dorisStep%:*}"
  inputFileName=`eval echo ${dorisStep#*:}`
  [ ! -e "${inputFileName}" ] && { error "Can not find specified file: ${inputFileName}"; return 1; }
fi

call "(fileName resFile format width numlines)=dorisProcess2OutputFile ${dorisStep}"
# if not empty and different from read value. 
if [ -n "${w}" ] && [ "${w}" != "${width}" ]; then
  error "Overwriting width(${width}) with user specified value: ${w}"
  width=${w};
fi  
if [ -n "${f}" ] && [ "${f}" != "${format}" ]; then
    error "Overwriting format(${format}) with user specified value: ${f}"
    format=${f};
fi

#if specified change fileName to inputFileName
[ "${inputFileName:-undefined}" != "undefined" ] && fileName=${inputFileName};

if [ -z ${fileName} ]; then
  error "No data file for the step ${dorisStep} in ${resFile}"
  return;
fi

error Image Width  ..${width}..
error Image Format ..${format}..
outName=`basename ${fileName}| tr '.' '_'`
[ $? -ne 0 ] && { error "I have problems finding filename: ${fileName}"; return 1; }
cmd=`echo "${ADOREFOLDER}/lib/python/deramp.py -w ${width} -f ${format} ${remainder} ${fileName}"`
eval "${cmd}"

# Rename the output to the deramped file in the result file. Only if the user did not specify a different file. 
[ "${inputFileName:-undefined}" == "undefined" ] && modifyRes.sh "${resFile}" "${dorisStep}" "Data_output_file" "${fileName}deramp"
# go back to adore
return 0;

